package com.ruiysoft.crm.service.impl;

import cn.taroco.common.vo.UserVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruiysoft.crm.mapper.BizBookingClientMapper;
import com.ruiysoft.crm.mapper.SysUserMapper;
import com.ruiysoft.crm.model.dto.BizBookingClientDTO;
import com.ruiysoft.crm.model.entity.BizBookingClient;
import com.ruiysoft.crm.service.BizBookingClientService;
import com.ruiysoft.crm.util.LocalDateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.sql.Wrapper;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fanyl
 */
@Service
public class BizBookingClientServiceImpl extends ServiceImpl<BizBookingClientMapper, BizBookingClient> implements BizBookingClientService {
    @Autowired
    private BizBookingClientMapper bizBookingClientMapper;
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public Object addBookingByClientId(int client , LocalDateTime bookingTime, UserVO userVO) {
        if(userVO==null){
            userVO=new UserVO();
            userVO.setUsername("hanl");
        }
        userVO = sysUserMapper.getUsersByName(userVO.getUsername());
        bizBookingClientMapper.changeStatusByClientId(client);
        BizBookingClient bookingClient=new BizBookingClient();
        bookingClient.setBookingTime(bookingTime);
        bookingClient.setClientId(client);
        bookingClient.setReadStatus("0");//已读状态
//        bookingClient.setCarNo(client.getCarNo());
//        bookingClient.setCarOwner(client.getCarOwner());
//        bookingClient.setMobile(client.getMobilePhone());
//        //bookingClient.setCarInsuranceDeadline(client.getcar);//车险到期日
        bookingClient.setUserName(userVO.getUsername());
        bookingClient.setUserId(userVO.getUserId());
        bookingClient.setCreateDate(LocalDateTime.now());
        bookingClient.setModificationDate(LocalDateTime.now());

        bizBookingClientMapper.insert(bookingClient);
        return bookingClient;
    }

    @Override
    public Object changeStatusById(String id) {
        bizBookingClientMapper.changeStatusById(id);
        return null;
    }

    @Override
    public Object getUnReadList(UserVO userVO) {
        if(userVO==null){
            userVO=new UserVO();
            userVO.setUsername("hanl");
        }
        userVO=sysUserMapper.getUsersByName(userVO.getUsername());
//        QueryWrapper<BizBookingClient> queryWrapper = new QueryWrapper<>() ;
//        queryWrapper.le("booking_time",LocalDateTime.now());
//        queryWrapper.eq("user_id",userVO.getUserId());
//        queryWrapper.eq(" ifnull(read_Status,0)","0");
        //bizBookingClientMapper.selectList(queryWrapper);
        Map<String, Object> params=new HashMap<>();
        params.put("bookingtime",LocalDateTime.now());
        params.put("user_id",userVO.getUserId());
        params.put("read_Status","0");
        return bizBookingClientMapper.queryUnReadBooking(params);
        //return bizBookingClientMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<BizBookingClientDTO> queryBookingInfo(IPage page, Map<String, Object> params) {
        params.remove("page");
        params.remove("pageSize");
        params.remove("current");
        String bookingTime= (String) params.get("bookingTime");
        String selectType= (String) params.get("selectType");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if(bookingTime!=null){
            LocalDateTime startDate;
            LocalDate endDate;
            String start="";
            String end="";
            if("1".equals(selectType)){
              //  endDate= LocalDateTime.parse(bookingTime,df).toLocalDate();
                endDate=LocalDateTime.parse(bookingTime,df).toLocalDate();
                start=endDate+" 00:00:00";
                end=endDate+" 23:59:59";
            }else {
               // startDate=LocalDateTime.parse("1970-01-01 00:00:00",df);
                endDate=LocalDateTime.parse(bookingTime,df).toLocalDate();
                start="1970-01-01 00:00:00";
                end=endDate+" 23:59:59";
            }
            params.put("booking.bookingTime","booking.booking_time BETWEEN '"+start+"' and '"+end+"'");
        }
        params.remove("bookingTime");
        params.remove("selectType");
        List<BizBookingClientDTO> ls= bizBookingClientMapper.queryBookingInfo(page,params);
        page.setRecords(ls);
        return page;
    }

    @Override
    public Object getBookingCountByDate(LocalDateTime date, String type,String userName) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDate;
        LocalDateTime endDate;
        if("1".equals(type)){ //查询当天
            startDate= LocalDateTimeUtils.getDayStart(date);
            endDate=LocalDateTimeUtils.getDayEnd(date);

        }else{//查询当前时间之前的
            startDate=LocalDateTime.parse("1970-01-01 00:00:00",df);
            endDate=LocalDateTimeUtils.getDayEnd(date);
        }
        UserVO userVO=sysUserMapper.getUsersByName(userName);
        int t= bizBookingClientMapper.getBookingCountByDate(startDate,endDate,userVO.getUserId()+"");
        return t;
    }
}